Skip to content

feat: propagate VarData.module_code into page module code#6456

Open
FarhanAliRaza wants to merge 3 commits intoreflex-dev:mainfrom
FarhanAliRaza:app-module-code-vardata
Open

feat: propagate VarData.module_code into page module code#6456
FarhanAliRaza wants to merge 3 commits intoreflex-dev:mainfrom
FarhanAliRaza:app-module-code-vardata

Conversation

@FarhanAliRaza
Copy link
Copy Markdown
Contributor

@FarhanAliRaza FarhanAliRaza commented May 5, 2026

Adds module_code on VarData so Vars can contribute top-of-file JS helpers/constants. The default collector and the legacy _get_all_custom_code path both pick it up, ensuring snippets carried by Vars on memoized stateful components aren't dropped from the memo file.

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

New Feature Submission:

  • Does your submission pass the tests?
  • Have you linted your code locally prior to submission?

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

closes #6219

Adds module_code on VarData so Vars can contribute top-of-file JS
helpers/constants. The default collector and the legacy
_get_all_custom_code path both pick it up, ensuring snippets carried
by Vars on memoized stateful components aren't dropped from the memo
file.
@FarhanAliRaza FarhanAliRaza requested a review from a team as a code owner May 5, 2026 06:53
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 5, 2026

Merging this PR will degrade performance by 23.15%

❌ 2 regressed benchmarks
✅ 15 untouched benchmarks
⏩ 2 skipped benchmarks1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
test_compile_page[_stateful_page] 1.7 ms 2.2 ms -21.79%
test_compile_page[_complicated_page] 8.4 ms 10.9 ms -23.15%

Comparing FarhanAliRaza:app-module-code-vardata (03be1ef) with main (a84e29b)

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 5, 2026

Greptile Summary

This PR adds a module_code: tuple[str, ...] field to VarData, allowing Vars to contribute top-of-file JS helpers/constants to the page module. Collection is wired into both the DefaultCollectorPlugin tree-walker path and the legacy _get_all_custom_code recursion used by the memo-compile pipeline, with deduplication via dict.fromkeys / setdefault throughout.

  • Two integration-test routes (/multi, /hook) are navigated via frontend_url + \"multi\" / frontend_url + \"hook\" without a leading slash or .removesuffix(\"/\"), producing malformed URLs when frontend_url has no trailing slash — the same pattern every other integration test in this repo avoids with .removesuffix(\"/\").

Confidence Score: 4/5

Core implementation is correct and well-tested; the only defect is in the new integration test's URL construction, which would cause two of the three new playwright tests to navigate to wrong addresses.

The module_code field, its merge logic, and all three collection paths are implemented correctly. The integration test for /multi and /hook routes concatenates frontend_url directly with the path segment without a leading slash or .removesuffix('/'), so those tests would navigate to malformed URLs and fail to validate the behavior they were written to cover.

tests/integration/tests_playwright/test_var_module_code.py — both routed test functions need the URL construction fixed before the integration tests can be relied on.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/vars/base.py Adds module_code: tuple[str, ...] field to VarData with proper default, hook-merge propagation, deduplication in merge(), and __bool__ support. Implementation is clean and consistent with existing field patterns.
packages/reflex-base/src/reflex_base/components/component.py Adds _iter_var_module_code() that yields module_code from component Vars, internal hooks, and added hooks; integrates it into _get_all_custom_code() with deduplication via setdefault. Architecture is correct and mirrors existing custom-code patterns.
reflex/compiler/plugins/builtin.py Adds _collect_var_module_code() static method and calls it in both the regular and the fast-path compiled leave hook, matching the placement of _collect_component_custom_code. Correct and symmetric.
reflex/compiler/utils.py Adds _iter_var_module_code() call to _root_only_custom_code() so the memo-compile path picks up Var-level module snippets. Change is minimal and correctly placed.
tests/integration/tests_playwright/test_var_module_code.py New integration test covering the three cases. URL concatenation for the /multi and /hook routes is missing a leading slash separator, which will cause tests to navigate to malformed URLs if frontend_url has no trailing slash.
tests/units/compiler/test_plugins.py Adds unit test for the DefaultCollectorPlugin collecting var module_code. Straightforward and covers the new code path.
tests/units/components/test_component.py Adds unit test confirming that var module_code reaches the legacy _get_all_custom_code() path used by the memo compile pipeline.
tests/units/test_var.py Adds three focused unit tests for module_code default/truthiness, merge deduplication with order preservation, and hook-VarData propagation. Coverage is thorough for the new field.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["VarData\nmodule_code field"] -->|merge / dedup| B["VarData.merge"]
    B --> C["Var._get_all_var_data"]
    C --> D["Component._iter_var_module_code"]
    D --> E["Legacy path\n_get_all_custom_code"]
    D --> F["Plugin path\n_collect_var_module_code"]
    D --> G["Memo path\n_root_only_custom_code"]
    E --> H["Page module_code dict"]
    F --> H
    G --> H
Loading

Reviews (1): Last reviewed commit: "feat: propagate VarData.module_code into..." | Re-trigger Greptile

Comment thread tests/integration/tests_playwright/test_var_module_code.py
Comment thread tests/integration/tests_playwright/test_var_module_code.py
FarhanAliRaza and others added 2 commits May 5, 2026 11:59
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler: Add module_code field to VarData

1 participant